﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using GameLib.Utils.Pathing;
using GameLib.Utils;
using GameLib;
using Sandbox.Scene;
using System.Windows;
using Sandbox.Utils;
using GameLib.WorldMap;

namespace Sandbox.Editor.Helpers
{
	public class RiverNodeSemantics : IPathNodeSemantics<MapVertex>
	{
		public MapScene MapScene
		{
			get;
			set;
		}

		#region IPathNodeSemantics<MapVertex> Members

		public bool Equals(MapVertex thisNode, MapVertex otherNode)
		{
			return thisNode == otherNode;
		}

		public IEnumerable<MapVertex> GetNeighbours(MapVertex node)
		{
			foreach (MapVertex neighbour in node.Neighbours)
			{
				if (neighbour.IsWithinMapBounds(MapScene.Map))
					yield return neighbour;
			}
		}

		public bool CanEnter(MapVertex node)
		{
			foreach (MapVertex neighbour in GetNeighbours(node))
			{
				MapEdge edge = new MapEdge(node, neighbour);

				Tile tileA = MapScene.Map.GetTile(edge.TileA);
				Tile tileB = MapScene.Map.GetTile(edge.TileB);
				MapEdgeDirection dir = edge.Direction;

				if (tileA != null)
				{
					if (tileA.GetRiver(dir) != null)
						return false;
				}
				else if (tileB != null)
				{
					if (tileB.GetRiver(dir.Opposite) != null)
						return false;
				}

			}

			return true;
		}

		public float GetPathCost(MapVertex from, MapVertex to)
		{
			MapEdge edge = new MapEdge(from, to);

			Tile tileA = MapScene.Map.GetTile(edge.TileA);
			Tile tileB = MapScene.Map.GetTile(edge.TileB);

			if (tileA == null || tileB == null)
				return -1;

			return (float)(MapScene.GetVertexPoint(from) - MapScene.GetVertexPoint(to)).Length;
		}

		public float GetHeuristicCost(MapVertex from, MapVertex to, MapVertex goal)
		{
			return (float)(MapScene.GetVertexPoint(to) - MapScene.GetVertexPoint(goal)).Length * HEURISTIC_COST_MULT;
		}

		#endregion

		const float HEURISTIC_COST_MULT = 1.5f;
	}
}
